package org.jeecg.modules.factor.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;

import org.jeecg.modules.api.DTO.EmissionFactorQueryDTO;
import org.jeecg.modules.api.VO.EmissionFactorQueryVO;
import org.jeecg.modules.api.VO.EmissionFactorVO;

import org.jeecg.modules.api.entity.CyEmissionFactor;
import org.jeecg.modules.api.entity.CyEmissionFactorCollect;
import org.jeecg.modules.api.entity.CyEmissionFactorData;
import org.jeecg.modules.api.entity.CyHouseGas;
import org.jeecg.modules.factor.mapper.CyEmissionFactorCollectMapper;
import org.jeecg.modules.factor.mapper.CyEmissionFactorDataMapper;
import org.jeecg.modules.factor.mapper.CyEmissionFactorMapper;
import org.jeecg.modules.factor.mapper.CyHouseGasMapper;
import org.jeecg.modules.factor.service.ICyEmissionFactorCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 碳排因子收藏表
 * @Author: jeecg-boot
 * @Date:   2024-11-11
 * @Version: V1.0
 */
@Service
public class CyEmissionFactorCollectServiceImpl extends ServiceImpl<CyEmissionFactorCollectMapper, CyEmissionFactorCollect> implements ICyEmissionFactorCollectService {

    @Autowired
    private CyEmissionFactorCollectMapper cyEmissionFactorCollectMapper;

    @Autowired
    private CyEmissionFactorMapper cyEmissionFactorMapper;   //碳排因子mapper对象

    @Autowired
    private CyEmissionFactorDataMapper cyEmissionFactorDataMapper;    //碳排因子数据详情mapper对象

    @Autowired
    private CyHouseGasMapper cyHouseGasMapper;  //碳排因子温室气体配比mapper对象

    @Override
    public EmissionFactorVO queryById(String id) {

        //获取当前用户的组织ID和部门ID
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String organizationId = sysUser.getOrganizationId();
        String deptId = sysUser.getDeptId();

        //查询出当前用户收藏的指定碳排因子对象
        CyEmissionFactorCollect cyEmissionFactorCollect = cyEmissionFactorCollectMapper.queryById(id,organizationId,deptId);

        CyEmissionFactor cyEmissionFactor = new CyEmissionFactor();
        CyEmissionFactorData cyEmissionFactorData = new CyEmissionFactorData();
        List<CyHouseGas> gasList = new ArrayList<CyHouseGas>();

        //如果没有查询出该收藏对象，则不需要查询对应的碳排因子对象
        if(cyEmissionFactorCollect!=null){
            //根据查询出的碳排因子收藏对象的碳排因子id查询出对应的碳排因子对象
            cyEmissionFactor = cyEmissionFactorMapper.selectById(cyEmissionFactorCollect.getEmissionFactorId());
            //根据碳排因子id查询出对应的碳排因子数据详情对象
            cyEmissionFactorData =  cyEmissionFactorDataMapper.SelectByEmissionFactorId(cyEmissionFactorCollect.getEmissionFactorId());
            //根据碳排因子id查询出对应的温室气体配比对象集合
            gasList = cyHouseGasMapper.getGasList(cyEmissionFactorCollect.getEmissionFactorId());
        }

        EmissionFactorVO emissionFactorVO = new EmissionFactorVO();   //构造vo对象
        emissionFactorVO.setEmissionFactorObject(cyEmissionFactor);   //设置因子主体对象
        emissionFactorVO.setEmissionFactorDataObject(cyEmissionFactorData);  // 设置因子数据详情对象
        emissionFactorVO.setGasList(gasList);    // 设置温室气体配比对象
        return emissionFactorVO;
    }

    @Override
    public  IPage<EmissionFactorQueryVO>  queryPageList(EmissionFactorQueryDTO emissionFactorQueryDTO, Integer pageNo, Integer pageSize) {

        //获取当前用户的组织ID和部门ID
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        emissionFactorQueryDTO.setOrganizationId(sysUser.getOrganizationId());
        emissionFactorQueryDTO.setDeptId(sysUser.getDeptId());

        Page<EmissionFactorQueryVO> page = new Page<EmissionFactorQueryVO>(pageNo, pageSize);
        //查询出当前组织收藏的碳排因子
        IPage<EmissionFactorQueryVO> emissionFactorQueryVOIPage = cyEmissionFactorCollectMapper.queryPageList(page, emissionFactorQueryDTO);
        return emissionFactorQueryVOIPage;
    }

    @Override
    public String add(String emissionFactorId) {
        //查询出当前要收藏的碳排因子对象
        CyEmissionFactor cyEmissionFactor = cyEmissionFactorMapper.selectById(emissionFactorId);
        // 判断当前要收藏的碳排因子的类型，如果为组织因子库的因子则不能收藏（碳排因子库=1，组织因子库=2）
        if(cyEmissionFactor.getType().equals("2")){
            return "只能收藏碳排因子库中的因子";
        }
        //获取当前用户
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //构造好查询条件
        QueryWrapper queryWrapper = new QueryWrapper<>();
        // 因子id查询条件
		queryWrapper.eq("emission_factor_id",emissionFactorId);
        // 组织id查询条件
        queryWrapper.eq("organization_id",sysUser.getOrganizationId());
        // 部门id查询条件
        queryWrapper.eq("dept_id",sysUser.getDeptId());
        //在收藏表中查询该组织是否收藏了该因子
        Long count = cyEmissionFactorCollectMapper.selectCount(queryWrapper);
        // 如果没有收藏过该碳排因子则收藏该因子
		if(count == 0){
			CyEmissionFactorCollect cyEmissionFactorCollect = new CyEmissionFactorCollect();  // 构造碳排因子收藏对象
			cyEmissionFactorCollect.setOrganizationId(sysUser.getOrganizationId());   //当前用户的组织ID
			cyEmissionFactorCollect.setDeptId(sysUser.getDeptId());   //当前用户的部门ID
			cyEmissionFactorCollect.setEmissionFactorId(emissionFactorId);   //收藏的因子id
            //收藏该因子
            cyEmissionFactorCollectMapper.insert(cyEmissionFactorCollect);
			return "收藏成功！";
		}else{
			return "该碳排因子已收藏！";
		}
    }

    @Override
    public void removeByEmissionFactorId(String emissionFactorId) {
        //获取当前用户的组织ID和部门ID
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String organizationId = sysUser.getOrganizationId();
        String deptId = sysUser.getDeptId();
        // 删除该组织下收藏的指定碳排因子
        cyEmissionFactorCollectMapper.removeByEmissionFactorId(emissionFactorId,organizationId,deptId);
    }
}
